package net.shopxx.controller.shop.member;

import java.math.BigDecimal;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import net.shopxx.Message;
import net.shopxx.constant.TaskConstant;
import net.shopxx.controller.shop.BaseController;
import net.shopxx.entity.Member;
import net.shopxx.entity.Task;
import net.shopxx.entity.Task.TaskStatus;
import net.shopxx.pager.Pageable;
import net.shopxx.service.MemberService;
import net.shopxx.service.TaskService;

@Controller("shopMemberTaskController")
@RequestMapping("/member/task")
public class TaskController extends BaseController {

	@Resource(name = "taskServiceImpl")
	private TaskService taskService;
	@Resource(name = "memberServiceImpl")
	private MemberService memberService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Integer pageNumber, ModelMap model) {
		Pageable pageable = new Pageable(pageNumber, 10);
		model.addAttribute("page", taskService.findPage(pageable));
		return "shop/member/task/list";
	}

	/**
	 * 管理
	 */
	@RequestMapping(value = "/manage", method = RequestMethod.GET)
	public String manage(Integer pageNumber, ModelMap model) {
		Pageable pageable = new Pageable(pageNumber, 10);
		Member m = memberService.getCurrent();
		if (m.getTaskPublisher()) {
			model.addAttribute("publisher", taskService.findPage(m, pageable, true));
		}

		model.addAttribute("operator", taskService.findPage(m, pageable, false));
		return "shop/member/task/manage";
	}

	/**
	 * 发布
	 */
	@RequestMapping(value = "/publish", method = RequestMethod.GET)
	public String publish(ModelMap model) {
		return "shop/member/task/publish";
	}

	/**
	 * 发布
	 */
	@RequestMapping(value = "/doPublish", method = RequestMethod.POST)
	public String doPublish(Task task, RedirectAttributes redirectAttributes) {
		if (!StringUtils.isNotBlank(task.getTitle()) || task.getPrice().compareTo(BigDecimal.ZERO) != 1) {
			addFlashMessage(redirectAttributes, Message.warn("任务数据输入错误！"));
			return "redirect:manage.jhtml";
		}
		task.setPublisher(memberService.getCurrent());
		task.setStatus(TaskStatus.init);
		taskService.save(task);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:manage.jhtml";
	}

	/**
	 * 查看广场任务详情
	 */
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String view(Long taskId, ModelMap model) {
		Task task = taskService.find(taskId);
		if (task != null) {
			model.addAttribute("task", task);
		}
		return "shop/member/task/view";
	}

	/**
	 * 查看任务详情
	 */
	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public String detail(Long taskId, String from, ModelMap model, RedirectAttributes redirectAttributes) {
		Task task = taskService.find(taskId);
		if (task != null) {
			// 发布者浏览时，该任务非当前用户所发布
			if (from.equals(TaskConstant.TASK_PUBLISHER_LABEL) && !task.getPublisher().getUsername().equals(memberService.getCurrentUsername())) {
				addFlashMessage(redirectAttributes, Message.warn("任务所有者错误！"));
				return "redirect:manage.jhtml";
			}

			// 操作者浏览时，该任务非当前用户所接收的任务
			if (from.equals(TaskConstant.TASK_OPERATOR_LABEL) && !task.getOperator().getUsername().equals(memberService.getCurrentUsername())) {
				addFlashMessage(redirectAttributes, Message.warn("任务所有者错误！"));
				return "redirect:manage.jhtml";
			}
			model.addAttribute("task", task);
		}else{
			addFlashMessage(redirectAttributes, Message.warn("无此任务！"));
			return "redirect:manage.jhtml";
		}
		return "shop/member/task/view";
	}

}
